(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:HongbaoCtrl
     * @description
     * # HongbaoCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('HongbaoCtrl', HongbaoCtrl);

    HongbaoCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', 'sessionService', 'cacheService'];

    function HongbaoCtrl($scope, dataService, helper, $rootScope, $location, sessionService, cacheService) {
        //活动列表
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList();
            $scope.$on('event:up-store-id', function() {
                $scope.getList();
            });
        }

        $scope.getList = function(pageNo) {
            var query = {};

            var key = $rootScope.store_id + "HongbaoCtrl";
            var cache = cacheService.get(key);
            if (pageNo || !cache) {
                $scope.pageNo = pageNo ? pageNo : 1;
                query.content = {
                    "page_no": $scope.pageNo,
                    "page_size": $scope.pageSize,
                    "store_id": $rootScope.store_id
                };
                cacheService.set(key, query);
            } else {
                query = clone(cache);
                $scope.pageNo = query.content.page_no;
                $scope.pageSize = query.content.page_size;
            }
            var querys = clone(query);
            dataService.hongbaoGet(querys).then(function(list) {
                if (list) {
                    $scope.hongbao_list = list.hongbao_list;
                    $scope.hongbao_count = list.total_count;
                }
            });
        }

        //红包领取详情
        $scope.detailAction = function() {
            $scope.pageSize = 10;
            $scope.hongbao_id = $location.search().hongbao_id;

            $scope.getDetail1(1);
        }

        $scope.getDetail1 = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    hongbao_id: $scope.hongbao_id,
                }
            };

            dataService.hongbaoDetailGet(query).then(function(list) {
                if (list) {
                    $scope.detail_list = list.detail_list;
                    $scope.detail_count = list.total_count;
                }
            });
        }

        //红包活动添加页面
        $scope.addAction = function() {
            var hongbao_id = $location.search().hongbao_id;
            if (hongbao_id) {
                var query = {
                    "content": {
                        "store_id": $rootScope.store_id,
                        "hongbao_id": hongbao_id
                    }
                };

                dataService.hongbaoGetOne(query).then(function(result) {
                    $scope.hongbao_data = result;
                    $scope.hongbao_data.store_id = $rootScope.store_id;
                    $scope.hongbao_id = result.hongbao_id;
                });
            } else {
                var data = {
                    "store_id": $rootScope.store_id,
                    "speech_content": "恭喜发财",
                    "start_time": $scope.getDateStr(0),
                    "end_time": $scope.getDateStr(7),
                    "give_way": 1, // 1=均价 2=随机
                    "red_envelope": 10,
                    "red_number": 10,
                };
                $scope.hongbao_data = data;
            }
        }

        //红包活动添加
        $scope.hongbaoAdd = function() {
            if ($scope.hongbao_data.end_time < $scope.hongbao_data.start_time) {
                helper.myAlert("error:红包活动结束时间不能小于活动开始时间");
                return false;
            }

            if (!$scope.hongbao_data.red_envelope) {
                helper.myAlert("error:红包金额不能为空！");
                return false;
            }

            if (!$scope.hongbao_data.red_number) {
                helper.myAlert("error:红包数量不能为空！");
                return false;
            }

            console.error("hongbao_data:", $scope.hongbao_data);
            // return false;

            var hongbao_id = $location.search().hongbao_id;
            if (hongbao_id) {
                dataService.hongbaoUpdate($scope.hongbao_data).then(function(result) {
                    if (result.hongbao_id) {
                        helper.myAlert("success", "#/hongbao/list");
                    }
                });
            } else {
                console.log("hongbao data--->", $scope.hongbao_data);
                // return false;
                dataService.hongbaoAdd($scope.hongbao_data).then(function(result) {
                    console.error("红包活动：", result);

                    if (result.hongbao_id) {
                        $scope.hongbao_id = result.hongbao_id;
                        helper.myAlert("success");
                        $scope.getList();
                    }
                });
            }
        }


        //砍价添加页面
        $scope.infoAction = function() {
            $scope.bargain_rule_id = $location.search().bargain_rule_id;
            $scope.stype = "KANJ";
            $scope.ffectget();
            $scope.infoGet(1);
        }

        $scope.ffectget = function() {
            var data = {
                bargain_rule_id: $scope.bargain_rule_id
            }
            dataService.ffectget(data).then(function(data) {
                $scope.total_bargain = data;
                // $scope.total_bargain.total_bargain_activity = 21;
            });
        }

        $scope.infoGet = function(pageNo) {
            var data = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.page_Size,
                query: {
                    bargain_rule_id: $scope.bargain_rule_id
                },
                order_by: "total_bargain_times:desc"
            }
            dataService.activityget(data).then(function(data) {
                $scope.bargain_activitys = data.bargain_activitys;
                $scope.total_count = data.total_count;
            });
        }

        //砍价活动暂停
        $scope.kanjiaPause = function(bargain_rule_id) {
            var query = {
                "query": {
                    "bargain_rule_id": bargain_rule_id
                }
            };

            dataService.kanjiaGetOne(query).then(function(result) {
                $scope.kanjia_data = result;
                $scope.kanjia_data.end_time = $scope.getDateStr(0);

                dataService.kanjiaUpdate($scope.kanjia_data).then(function(result) {
                    if (result.bargain_rule_id) {
                        $scope.getList();
                    }
                });
            });
        }

        //砍价活动删除
        $scope.kanjiaDelete = function(bargain_rule_id) {
            helper.myConfirm('确认删除吗？', function() {
                var data = {
                    'bargain_rule_id': bargain_rule_id
                };
                console.error("删除--->", data);
                // return false;
                dataService.kanjiaDelete(data).then(function(result) {
                    console.error("删除结果：", result);
                    // return false;
                    if (result.bargain_rule_id) {
                        $scope.getList(1);
                    } else {
                        helper.myAlert("error:" + result);
                    }
                });
            });
        }

        //活动时间计算
        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            var s = dd.getSeconds();

            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }

            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }

        //商品directives回调方法
        $scope.itemdata = function(items) {
            //商品信息是多维数组时
            if (items) {
                //选中商品的item_id sku_id
                $scope.kanjia_data.item_id = items.sku_data.item_id;
                $scope.kanjia_data.sku_id = items.sku_data.sku_id;

                //选中商品
                $scope.checked_item = items;

                console.error("砍价数据--->", $scope.kanjia_data);
            }
        }

        //余额提现
        $scope.withdrawallogAction = function() {
            $scope.pageSize = 10;

            $scope.getLogList(1);

            $scope.$on('event:up-store-id', function() {
                $scope.getLogList(1);
            });
        }

        $scope.getLogList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    store_id: $rootScope.store_id,
                }
            };

            dataService.withdrawalLogGet(query).then(function(list) {
                if (list) {
                    $scope.log_list = list.log_list;
                    $scope.log_count = list.total_count;
                }
            });
        }
    }
})();